package org.feidao.chapter91.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.feidao.chapter91.common.RestResult;
import org.feidao.chapter91.domain.Heroic;
import org.feidao.chapter91.elasticSearch.ElasticSearchConstants;
import org.feidao.chapter91.service.HeroicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by linghufeixia on 2023-2-17.
 */
@RestController
@RequestMapping(value = "elasticsearch")
public class ElasticsearchController {

    @Autowired
    private HeroicService heroicService;

    @RequestMapping(value = "/createIndex", method = RequestMethod.GET)
    public String createIndex(){
        heroicService.createIndex(ElasticSearchConstants.index);
        RestResult restResult = new RestResult();
        restResult.setCode(200);
        restResult.setMsg("create index success!");
        return JSON.toJSONString(restResult);
    }


    @RequestMapping(value = "/saveHeroic", method = RequestMethod.GET)
    public String saveHeroic(@RequestParam(value = "id") String id,
                           @RequestParam(value = "name") String name,
                           @RequestParam(value = "remarks") String remarks){
        Heroic heroic = new Heroic();
        heroic.id =id;
        heroic.name = name;
        heroic.remarks = remarks;

        String documentId = "1";
        heroicService.insertHeroic(heroic, ElasticSearchConstants.index, documentId);
        RestResult restResult = new RestResult();
        restResult.setCode(200);
        restResult.setMsg("save success!");
        return JSON.toJSONString(restResult);
    }

    @RequestMapping(value = "/selectHeroic", method = RequestMethod.GET)
    public String selectHeroic(){
        String documentId = "1";
        String context = heroicService.selectHeroic(ElasticSearchConstants.index, documentId);
        RestResult restResult = new RestResult();
        restResult.setCode(200);
        restResult.setMsg(context);
        return JSON.toJSONString(restResult);
    }


    @RequestMapping(value = "/updateHeroic")
    public String updateHeroic(@RequestParam(value = "id") String id,
                         @RequestParam(value = "name") String name,
                         @RequestParam(value = "remarks") String remarks){
        Heroic heroic = new Heroic();
        heroic.id =id;
        heroic.name = name;
        heroic.remarks = remarks;

        String documentId = "1";
        heroicService.updateHeroic(heroic, ElasticSearchConstants.index, documentId);

        RestResult restResult = new RestResult();
        restResult.setCode(200);
        restResult.setMsg("update success!");
        return JSON.toJSONString(restResult);
    }

    @RequestMapping(value = "/deleteHeroic")
    public String deleteHeroic( ){
        String documentId = "1";
        heroicService.deleteHeroic(ElasticSearchConstants.index, documentId);
        RestResult restResult = new RestResult();
        restResult.setCode(200);
        restResult.setMsg("delete success!");
        return JSON.toJSONString(restResult);
    }

}
